package main

import (
	"context"
	"github.com/segmentio/kafka-go"
	"log"
)

func main() {
	// Define the Kafka topic and address
	topic := "opreationlogs"
	address := []string{"10.0.12.38:9092", "10.0.12.39:9092", "10.0.12.40:9092"}

	// make a writer that produces to topic-A, using the least-bytes distribution
	w := &kafka.Writer{
		Addr:     kafka.TCP(address...),
		Topic:    topic,
		Balancer: &kafka.LeastBytes{},
	}

	/**
	 * 登录账号 | 登录账号ID | 操作账号 | 操作账号ID | 操作平台 | 服务名称 | 服务名称别名 | 操作分类 | 操作对象 | 操作时间 | 登录IP | 操作结果 | 失败原因 | 日志等级 | 日志类型           |操作描述
	 * zhangsan| 0ea52fa2 |zhangsan | 0ea52fa2 |   omc  | 角色管理 |    role    | 创建角色 | 名称+ID |2022-03-21|8.8.8.8|成功    |         |普通    |application-operate| 请求json
	 * gaozizhong|3c77c1bbbfca11ec9e660cda411d7aa5|gaozizhong|3c77c1bbbfca11ec9e660cda411d7aa5|omc|监控平台|unitrap|新建trap|名称+ID|2022-03-21||成功||普通|application-operate|请求json
	 */
	err := w.WriteMessages(context.Background(),
		kafka.Message{
			//Key:   []byte("Key-A"),
			//Value: []byte("gaozizhong|3c77c1bbbfca11ec9e660cda411d7aa5|gaozizhong|3c77c1bbbfca11ec9e660cda411d7aa5|omc" +
			//	"|监控平台|unitrap|新建trap|名称+ID|2022-03-21 11:30:05|8.8.8.8|成功|-|普通|application-operate|" +
			//	"{'moduleName': 'Trap管理->Trap定义'}"),
			Value: []byte("gaozizhong|3c77c1bbbfca11ec9e660cda411d7aa5|gaozizhong|3c77c1bbbfca11ec9e660cda411d7aa5|" +
				"omc|监控平台|unitrap|新建trap|名称+ID|2023-05-09 12:30:05|8.8.8.8|成功|-|普通|application-operate|{\"moduleName\": \"Trap管理->Trap定义\"}"),
		},
	)

	if err != nil {
		log.Fatal("failed to write messages:", err)
	}

	if err := w.Close(); err != nil {
		log.Fatal("failed to close writer:", err)
	}
}
